<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 *
 * Created on	:  Sep 13, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');

/**
 * The controller for State application
 *
 */
class VideoBackController extends AbstractBackEndController {

	var $_model;

	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('VideoModel');
		$this->_model = new VideoModel();
		$this->registerTask ( "new", "edit" );
		$this->registerTask ( "apply", "save" );
	}

	function display() {
		$this->setTitle ( "Manage Video" );
		$this->addToolbarButton ( "new" );
		$this->addToolbarButton ( "edit" );
		$this->addToolbarButton ( "delete" );
		$msg = SysRequest::getVar ( "msg" );
		RCoreImporter::cls( "html.TableControl" );
		RClassesImporter::cls("dbo.dtos.VideoDTO" );
		$table = new TableControl ( 'VideoDTO', NULL, $_REQUEST, 'VideoCtrl', '', '' );
		//TableControl($dtoName, $arrayViewField, $arrayRequest, $name, $where, $orderBy, $db=null)
		$nextExec = $this->getExecuteBy();
		$nextExec = ucfirst($nextExec);
		
		$table->removeField('video_id');
		$table->setLinkRule("des_en",'javascript:edit'.$nextExec.'Item($key)');
		$msg = "<p class=\"form-message\" style=\"text-align:left;\">
		{$msg}
			  </p>";
		$tblHtml = $table->createHtmlTable(20);
		eval('echo "'.SysApplication::loadTemplate('back/listVideo').'";');
	}




	function edit($dto = null) {
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if (empty( $dto )) {
			if(is_array($cid)) {
				$cid = $cid[0];
			}
			$dto = $oModel->getVideoDetails($cid);
		}
		RCoreImporter::cls( "html.FormFieldsControl" );
		$formData = new FormFieldsControl ( $dto );
		$formData = $this->_getEditFormAndPrepareEditTask($dto, 'link');
		//$formData->setFieldHtmlInfo('album_id',$this->_getNewsCbxFieldInfo('album_id'));
		//$formData->setFieldHtmlInfo('img_link',$this->_getPicFileFiledInfo('img_link'));
		$formData->setFieldHtmlInfo("des_en", $this->_getRichTextHtmlFieldInfo('des_en'));
		$formData->setFieldHtmlInfo("des_fr", $this->_getRichTextHtmlFieldInfo('des_fr'));
		$htmlContent = $formData->generateHtml();

		$msg = SysRequest::getVar('msg');
		eval('echo "'.SysApplication::loadTemplate('back/editVideo').'";');
	}



	function save($isVideoEdit = false) {
		$model = $this->_model;
		$dto = new VideoDTO();
		if (SysRequest::getInt('id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);
		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}
		//$picField = $this->_getPicFileFiledInfo('img_link'); 
		//$dto->img_link = $picField->getUploadedFilePath('files/Videos/');	
		$result = $model->saveVideoDTO ( $dto );
		if ($dto->hasError())
		$msg = $dto->getAllErrorsMsg();
		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		} else {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" ));
		}
	}
	function apply($isVideoEdit = false) {
		$model = $this->_model;
		$dto = new VideoDTO();
		if (SysRequest::getInt('id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);
		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}
		//$picField = $this->_getPicFileFiledInfo('img_link'); 
		//$dto->img_link = $picField->getUploadedFilePath('files/Videos/');	
		// files processing
		//$this->_setFileValue('icon', $dto, 'files/Videos/Video/thumbnail/', $_POST);

		$result = $model->saveVideoDTO ( $dto );
		if ($dto->hasError())
		$msg = $dto->getAllErrorsMsg();

		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		}
	}
	function &_getPicFileFiledInfo($inputName) {

		RCoreImporter::cls( "html.fieldInfo.FlashFileHtmlFieldInfo" );
		$field = new FlashFileHtmlFieldInfo($inputName,$linkRule);
		$field->setSupportedFilteType('.jpg,.jpeg,.bmp,.png,.gif');
		return $field;
	}
	function &_getNewsCbxFieldInfo($info) {
		$sql = "SELECT id, des_en FROM albums";
		$info = &$this->_getSqlComboboxHtmlFieldInfo($info, $sql);
		if ($arrayOrObject) {
			$info->setValue($arrayOrObject);
		}
		return $info;
	}

	function delete() {
		$model = $this->_model;
		$arrayIds = SysRequest::getVar ( "cid" );
		$model->delete($arrayIds);
		$this->setRedirectToFunction('display', SysLanguage::_ ( "ITEM_DELETE_SUCCESSFUL" ));
	}
}
